package com.teangtang.fly.controller;
import com.teangtang.fly.dto.FlightDto;
import com.teangtang.fly.entity.*;
import com.teangtang.fly.service.AirlineService;
import com.teangtang.fly.service.CabinService;
import com.teangtang.fly.service.FlightService;
import com.teangtang.fly.service.PlaneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

@RestController
@RequestMapping("/flight")
public class FlightController {

    @Autowired
    private FlightService flightService;

    @Autowired
    private PlaneService planeService;

    @Autowired
    private AirlineService airlineService;

    @Autowired
    private CabinService cabinService;

    @GetMapping
    public ResponseEntity<List<FlightDto>> getAllFlight() {
        List<Flight> flights = flightService.getAllFlight();
        List<FlightDto> flightDtos = flightService.turnFlightsToFlightDto(flights,0);
        return ResponseEntity.ok(flightDtos);
    }

    @GetMapping("/{flightId}")
    public ResponseEntity<FlightDto> getFlightByFlightId(@PathVariable("flightId") String flightId) {
        Flight flight = flightService.getFlightByFlightNumber(flightId);
        List<Flight> flights = new ArrayList<>();
        flights.add(flight);
        FlightDto flightDto = flightService.turnFlightsToFlightDto(flights,1).get(0);
        return ResponseEntity.ok(flightDto);
    }

    @PostMapping
    public ResponseEntity<Flight> createFlight(@RequestBody FlightDto flightDto) {
        Plane plane = planeService.getPlaneById(flightDto.getPlane());
        Airline airline = airlineService.getAirlineByAirlineNumber(flightDto.getAirline());
        Flight flight = new Flight();
        flight.setPlane(plane);
        flight.setAirline(airline);
        flight.setCompanyName(flightDto.getCompanyName());
        flight.setFlightNumber(flightDto.getFlightNumber());
        flight.setDepartureTime(flightDto.getDepartureTime());
        flight.setArrivalTime(flightDto.getArrivalTime());
        flight.setFrequency(flightDto.getFrequency());
        List<CabinPrice> cabinPrices = new ArrayList<>();
        for(int i=0;i<flightDto.getCabinPrices().size();i++){
            CabinPrice cabinPrice = new CabinPrice();
            Cabin cabin = cabinService.getCabinById(flightDto.getCabinPrices().get(i).getId());
            cabinPrice.setCabin(cabin);
            cabinPrice.setPrice(flightDto.getCabinPrices().get(i).getPrice());
            cabinPrices.add(cabinPrice);
        }
        if (flight.getCabinPrices() == null) {
            flight.setCabinPrices(new ArrayList<>());
        }
        flight.getCabinPrices().addAll(cabinPrices);
        for(CabinPrice cabinPrice:flight.getCabinPrices()){
            cabinPrice.setFlight(flight);
        }
        flight.setStatus("0");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(flight.getDepartureTime());
        flight.setStartDate(calendar.getTime());
        flight.setFlightId(flightService.generateFlightId(flight));
        flight=flightService.createFlight(flight);
        flightService.generateFlightSchedule(flight);
        return ResponseEntity.ok(flight) ;
    }
    @DeleteMapping("/{flightNumber}")
    public void deleteVoyage(@PathVariable String flightNumber) {
        flightService.deleteFlight(flightNumber);
    }
}
